local TALENT_GRID = {}

function TALENT_GRID:Init()
	self.Icon = "devin/blackbetty/hudicons/inventoryslot.png"
	self.NumCol = 10
	self.NumRows = 1
	self.CellSize = 48
	self.HoveredCell = 0
	self.OldHoveredCell = 1
	self.CellInfo = {}
	self.CellInfo[1] = {}
	self.CellInfo[1].TierNumber = 0
	self.CellInfo[1].X_Offset = 0
	self.CellInfo[1].Y_Offset = 0
	self.CellInfo[1].Color = Color(255, 255, 255, 255)
	self.ClickedCell = true
	self.CellToDraw = 1
	self.OldSelectedTier = 1
	self.SelectedTier = 1
	self:CreateCells()
	self:RebuildTalents()
end

function TALENT_GRID:SetCellSize(size)
	self.CellSize = size
	self:CreateCells()
	self:RebuildTalents()
end

function TALENT_GRID:SetCols(cols)
	self.NumCol = cols
	self:CreateCells()
	self:RebuildTalents()
end

function TALENT_GRID:SetRows(rows)
	self.NumRows = rows
	self:CreateCells()
	self:RebuildTalents()
end


function TALENT_GRID:CreateCells()
	self.CellInfo = {}
	local CellNum = 1
	for i = 1, self.NumRows, 1 do
		for k = 1, self.NumCol, 1 do
			self.CellInfo[CellNum] = {}
			self.CellInfo[CellNum].TierNumber = 0
			self.CellInfo[CellNum].X_Offset = ((k * self.CellSize)-self.CellSize)
			self.CellInfo[CellNum].Y_Offset = ((i * self.CellSize)-self.CellSize)
			self.CellInfo[CellNum].Color = Color(255, 255, 255, 255)
			self.CellInfo[CellNum].DefaultColor = Color(255, 255, 255, 255)
			self.CellInfo[CellNum].Icon = "devin/blackbetty/hudicons/inventoryslot.png"
			CellNum = CellNum + 1
		end
	end
end

function TALENT_GRID:CalcCells(talents)
	local cell_inc = 0
	if(#talents>0)then
		for k, v in pairs(talents)do
			if(v)then
				PrintTable(v)
				cell_inc = cell_inc + 1
			end
		end
		self:SetCols(cell_inc)
	end
end

function TALENT_GRID:RebuildTalents()
	PrintTable(selectedTalents)
	if(selectedTalents and #selectedTalents > 0)then
		for k, v in pairs(self.CellInfo)do
			if(selectedTalents[k] and selectedTalents[k] != 0)then 
				v.Icon = GetSpellByID(ClassData.LevelData[playerClass][k][2][1][selectedTalents[k]])[2]
				v.DefaultColor = Color(255, 255, 255, 255)
				v.Color = Color(255, 255, 255, 255)
			elseif(selectedTalents[k] and selectedTalents[k] == 0)then
				v.Icon = "devin/blackbetty/hudicons/inventoryslot.png"
				v.DefaultColor = Color(255, 255, 225, 255)
				v.Color = Color(255, 255, 225, 255)
			elseif(!selectedTalents[k])then
				v.DefaultColor = Color(255, 155, 155, 255)
				v.Color = Color(255, 155, 155, 255)
			end
		end
	end
end

function TALENT_GRID:Paint()
	for i = 1, #self.CellInfo, 1 do
		surface.SetMaterial(Material( self.CellInfo[i].Icon ) )
		if(self.SelectedTier == i)then
			surface.SetDrawColor(Color(155, 155, 255, 255))
		else
			surface.SetDrawColor(self.CellInfo[i].Color)
		end
		surface.DrawTexturedRect(self.CellInfo[i].X_Offset, self.CellInfo[i].Y_Offset, self.CellSize, self.CellSize)
	end
end

function TALENT_GRID:Think()
	local xpos, ypos = self:LocalCursorPos()
	local xmult_ = math.Round((xpos/self.CellSize)+0.5, 0)		
	local ymult_ = math.Round((ypos/self.CellSize)+0.5, 0)
	local xmult = math.Clamp(xmult_, 0, self.NumCol)
	local ymult = math.Clamp(ymult_, 0, self.NumRows)
	if((xpos >= 0 and ypos >= 0) and ((xpos <= self.CellSize * self.NumCol) and (ypos <= self.CellSize * self.NumRows)))then
		self.HoveredCell = math.Round((xmult + ((ymult*self.NumCol)-self.NumCol)), 0)
		self.CellInfo[self.HoveredCell].Color = Color(155, 255, 155, 255)
		if(input.IsMouseDown(MOUSE_LEFT))then
			self.SelectedTier = self.HoveredCell
			if(!self.ClickedCell)then
				self.ClickedCell = true
				surface.PlaySound("buttons/lightswitch2.wav", 60, 10000)
			end
		else
			self.ClickedCell = false
		end
	end
	if(self.HoveredCell != self.OldHoveredCell and self.HoveredCell > 0 or !(((xpos <= self.CellSize * self.NumCol) and (ypos <= self.CellSize * self.NumRows))))then
		self.CellInfo[self.OldHoveredCell].Color = self.CellInfo[self.OldHoveredCell].DefaultColor
		if(self.HoveredCell > 0)then
			self.OldHoveredCell = self.HoveredCell
		end
	end
end

vgui.Register( "TalentGrid", TALENT_GRID, "DImage")